package cn.tedu.straw.portal.controller;

import cn.tedu.straw.portal.service.ex.*;
import cn.tedu.straw.commons.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author shkstart
 * @create 2020-07-17 10:40
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler
    public R handleException(Throwable e){
        if (e instanceof ParameterValidationException) {
            return R.failure(R.State.ERR_PARAMETER_INVALIDATION, e);
        }else if(e instanceof InviteCodeException){
            return R.failure(R.State.ERR_INVITE_COOE, e);
        }else  if(e instanceof ClassDisabledException){
            return R.failure(R.State.ERR_CLASS_DISABLED, e);
        }else if(e instanceof PhoneDuplicateException){
            return R.failure(R.State.ERR_PHONE_DUPLITCATE, e);
        }else if(e instanceof InsertException){
            return R.failure(R.State.ERR_INSERT, e);
        }else if (e instanceof AccessDeniedException){
            return R.failure(R.State.ERR_ACCESS_DENIDE, e);
        }else if(e instanceof FileEmptyException){
            return R.failure(R.State.ERR_FILE_EMPTY, e);
        }else if(e instanceof FileSizeException){
            return R.failure(R.State.ERR_FILE_SIZE, e);
        }else if(e instanceof FileTypeException){
            return R.failure(R.State.ERR_FILE_TYPE, e);
        }else if(e instanceof FileIOException){
            return R.failure(R.State.ERR_FILE_IOE, e);
        } else if(e instanceof QuestionNotFoundException){
            return R.failure(R.State.ERR_QUESTION_NOTFOUND, e);
        } else if (e instanceof CommentNotFoundException){
            return R.failure(R.State.ERR_COMMENT_NOT_FOUND, e);
        } else if (e instanceof PermissionDeniedException){
            return R.failure(R.State.ERR_PERMISSION_DENIED, e);
        } else if (e instanceof DeleteException){
            return R.failure(R.State.ERR_DELETE, e);
        }else if (e instanceof SelectExcption){
            return R.failure(R.State.ERR_SELECT, e);
        }else if (e instanceof AnswerNotFoundException){
            return R.failure(R.State.ERR_ANSWER_NOT_FOUND, e);
        }else{
            log.debug("unkonwn exeception > {}", e);
            return R.failure(R.State.ERR_UNKNOWN, e);
        }
    }
}
